package com.test.service;

import com.test.dao.EmployeeDao;
import com.test.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

@Component
public class EmployeeService {

    private final EmployeeDao employeeDao;

    @Autowired

    public EmployeeService(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    public int Login(Employee employee,String key) throws NoSuchAlgorithmException {
        System.out.println(key);
        if (Objects.equals(key, "t1kVlxbj")){
            int i = employeeDao.selectByEmployeeNumber(employee.getEmployeeNumber());
            String password_old = employee.getPassword();
            MessageDigest md = MessageDigest.getInstance("SHA");
            byte[] digest = md.digest(password_old.getBytes());
            BigInteger bigInteger = new BigInteger(1, digest);
            String password_new = bigInteger.toString(16);
            System.out.println(bigInteger.toString(16));
            if (i==1){//登录
                String password_d= employeeDao.selectPasswordByEmployeeNumber(employee.getEmployeeNumber());
                if (password_d.equals(password_new)) return 1;
                else return 3;
            }
            else {//注册
                employee.setPassword(password_new);
                employeeDao.insertEmployee(employee);
                return 1;

            }

        }
        else return 2;
    }

}
